package com.stj.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JdbcUtil {
    //连接参数 jdbc四要素：Driver,url,user,password
    private static String driver="com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/db_blog3?characterEncoding=utf8&serverTimezone=GMT%2B8";
    private static String name = "root";
    private static String password = "123456";
    //1.连接数据库
    public static Connection  getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,name,password);
        } catch (Exception e) {
            System.out.println("数据库连接错误"+e);
        }
        return conn;
    }
    //2.关闭资源
    public static void myClose(Connection conn, PreparedStatement pst){
        try{
            if (pst!=null){
                pst.close();
            }
            if (conn!=null){
                conn.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
